Conditions | 14 |
Total Lines | 77 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like helpers.js ➔ uncloakLinks often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
156 | export async function uncloakLinks(attribute = "data-rot") { |
||
157 | var convertLink = function(element) { |
||
158 | // fix "bug" with img |
||
159 | if (element.getAttribute(attribute) === null) { |
||
160 | var element = element.closest("[" + attribute + "]"); |
||
161 | } |
||
162 | if (element.getAttribute(attribute) === null) return; |
||
163 | var link = document.createElement("a"); |
||
164 | var href = element.getAttribute(attribute); |
||
165 | element.removeAttribute(attribute); |
||
166 | for (var i = 0, n = element.attributes.length; i < n; i++) { |
||
167 | link.setAttribute( |
||
168 | element.attributes[i].nodeName, |
||
169 | element.attributes[i].nodeValue |
||
170 | ); |
||
171 | } |
||
172 | link.innerHTML = element.innerHTML; |
||
173 | link.setAttribute( |
||
174 | "href", |
||
175 | responsiveImage(convertShortchutForLink(rot13ToText(href))) |
||
176 | ); |
||
177 | element.parentNode.replaceChild(link, element); |
||
178 | return link; |
||
179 | }; |
||
180 | |||
181 | var convertThemAll = function(attribute) { |
||
182 | [].forEach.call(document.querySelectorAll("[" + attribute + "]"), function( |
||
183 | element |
||
184 | ) { |
||
185 | convertLink(element); |
||
186 | }); |
||
187 | }; |
||
188 | |||
189 | var fireEventLinksBuilt = async function(element, event) { |
||
190 | await document.dispatchEvent(new Event("linksBuilt")); |
||
191 | |||
192 | var clickEvent = new Event(event.type); |
||
193 | element.dispatchEvent(clickEvent); |
||
194 | }; |
||
195 | |||
196 | var convertLinkOnEvent = async function(event) { |
||
197 | // convert them all if it's an image (thanks this bug), permit to use gallery (baguetteBox) |
||
198 | if (event.target.tagName == "IMG") { |
||
199 | await convertThemAll(attribute); |
||
200 | var element = event.target; |
||
201 | } else { |
||
202 | var element = convertLink(event.target); |
||
203 | } |
||
204 | fireEventLinksBuilt(element, event); |
||
205 | }; |
||
206 | |||
207 | [].forEach.call(document.querySelectorAll("[" + attribute + "]"), function( |
||
208 | element |
||
209 | ) { |
||
210 | element.addEventListener( |
||
211 | "touchstart", |
||
212 | function(e) { |
||
213 | convertLinkOnEvent(e); |
||
214 | }, |
||
215 | { once: true } |
||
216 | ); |
||
217 | element.addEventListener( |
||
218 | "click", |
||
219 | function(e) { |
||
220 | convertLinkOnFly(e); |
||
221 | }, |
||
222 | { once: true } |
||
223 | ); |
||
224 | element.addEventListener( |
||
225 | "mouseover", |
||
226 | function(e) { |
||
227 | convertInLinksRot13OnFly(e); |
||
228 | }, |
||
229 | { once: true } |
||
230 | ); |
||
231 | }); |
||
232 | } |
||
233 | |||
317 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.